<script>
	var main, receiver, filter;

	export default {
		onLaunch: function() {
			this.initScan()
		},
		onShow: function() {
			if (this.token) {
				this.$store.commit('setUserInfo')
			}
			this.startScan()
		},
		onUnload() {
			this.stopScan()
		},
		onHide() {
			this.stopScan();
		},
		computed: {
			token() {
				return this.$store.state.token
			}
		},
		methods: {
			initScan() {
				let _this = this;
				/* #ifdef APP-PLUS */
				main = plus.android.runtimeMainActivity(); //获取activity  
				/* var context = plus.android.importClass('android.content.Context'); */
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				/* var Intent = plus.android.importClass('android.content.Intent'); */
				filter = new IntentFilter();
				filter.addAction("com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED");
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						/*这个se4500很坑，不同的手机或pda，这个值就不一样，要具体去查硬件api*/
						let code = intent.getStringExtra("data");
						/* rmker.showTip('条码:'+code); */
						/*调用本页面某方法*/
						_this.queryCode(code);
					}
				});
				/* #endif */
			},
			startScan() {
				/* #ifdef APP-PLUS */
				main.registerReceiver(receiver, filter);
				/* #endif */
			},
			stopScan() {
				/* #ifdef APP-PLUS */
				main.unregisterReceiver(receiver);
				/* #endif */
			},
			queryCode: function(data) {
				//防重复  
				var pages = getCurrentPages();
				const route = pages[pages.length - 1].route; //获取当前页面路由

				if (data.indexOf('code') > -1) {
					const obj = JSON.parse(data);
					const code = obj.code;
					const type = obj.type;
					//code = encodeURIComponent(code)
					if (type === 1 && (route === 'pages/index/index' || route === 'subPage_receiptOrder/index')) {
						console.log(route)
						uni.navigateTo({
							url: '/subPage_receiptOrder/receipt?code=' + code
						})
						//pages[pages.length - 1].$vm.getAccount(code)
					}
				} else if (data.length === 19) {
					if (route === 'pages/index/index' || route === 'subPage_testing/index') {
						uni.navigateTo({
							url: '/subPage_testing/testingIndex?code=' + data
						})
					} else if (route === 'subPage_testing/testingIndex') {
						pages[pages.length - 1].$vm.scanCode(data)
					}
				}
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import './global.css';


	html,
	body {
		background-color: #E6EDF2;
		font-family: 'HarmonyOS';
	}

	view,
	text {
		font-family: 'HarmonyOS';
	}

	page {
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	scroll-view::-webkit-scrollbar {
		display: none !important;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	::-webkit-scrollbar {
		display: none;
	}
</style>